package practise.alogrithm.hundredInterviewQuestions.secondTime;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Richard on 7/27/2014.
 */
public class NumberDuplicateMoreThanHalfArray {
    public Integer find(List<Integer> array){
        int count = 0;
        int number =-1;
        for(int index = 1; index<array.size();index++){
            if(array.get(index-1)==array.get(index)){
                count++;
            }
            else{
                count--;
            }
            if(count>0){
                number = array.get(index);
            }

        }

        //verify

        count=0;
        for(int index=0; index<array.size();index++){
            if(array.get(index)==number){
                count++;
            }
        }
        if(count>array.size()/2){
            return number;
        }
        else {
            return -1;
        }
    }

    public static void main(String[] args){
        System.out.println(new NumberDuplicateMoreThanHalfArray().find(new ArrayList<Integer>(Arrays.asList(new Integer[]{2,3,4,5,6,7,8,1,1,1,1,1,1,1,1,1,1,1}))));
    }
}
